package io.wilson.rabbit.config;

import io.wilson.rabbit.common.constant.RabbitMQConstant;
import io.wilson.rabbit.common.constant.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 直连型配置范例
 * <li> 队列与Exchange绑定，且要指定RoutingKey</li>
 * <li> 消息生产端向Exchange发送消息时，需指定消息的RoutingKey，消息会被绑定了该RoutingKey的所有队列消费</li>
 * <li> Exchange根据消息的RoutingKey判断，队列的RoutingKey与消息的RoutingKey完全一致才会收到消息</li>
 */
@Slf4j
@Configuration
public class DirectConfig {
    @Resource
    private SpringContextUtils springContextUtils;
    @Resource
    private ConnectionFactory connectionFactory;

    @PostConstruct
    public void init() {
        DirectExchange exchange = new DirectExchange(RabbitMQConstant.SEQUENCE_DIRECT_EXCHANGE);
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        for (int i = 0; i < 3; i++) {
            String queueName = RabbitMQConstant.SEQUENCE_DIRECT_QUEUE + i;
            // 创建队列
            Queue queue = new Queue(queueName);
            rabbitAdmin.declareQueue(queue);
            // 绑定队列到交换机
            rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("sequence-routing-" + i));
        }
/*        Exchange exchange = new DirectExchange(RabbitMQConstant.SEQUENCE_DIRECT_EXCHANGE);
        springContextUtils.registerSingletonBean(RabbitMQConstant.SEQUENCE_DIRECT_EXCHANGE, new DirectExchange(RabbitMQConstant.DIRECT_EXCHANGE_NAME));
        for (int i = 0; i < 3; i++) {
            String queueName = RabbitMQConstant.SEQUENCE_DIRECT_QUEUE + i;
            Queue queue = new Queue(queueName);
            springContextUtils.registerSingletonBean(queueName, queue);
            springContextUtils.registerSingletonBean("binding-" + i,
                    BindingBuilder.bind(queue).to(exchange).with("sequence-routing-" + i));
        }*/
        log.info("dynamic direct init completed");
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(RabbitMQConstant.DIRECT_EXCHANGE_NAME);
    }

    @Bean
    public Queue directQueueA() {
        return new Queue(RabbitMQConstant.DIRECT_QUEUE_NAME_1);
    }

    @Bean
    public Queue directQueueB() {
        return new Queue(RabbitMQConstant.DIRECT_QUEUE_NAME_2);
    }

    @Bean
    public Binding directBindingA(Queue directQueueA, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueueA)
                .to(directExchange).with(RabbitMQConstant.DIRECT_ROUTING_NAME);
    }

    @Bean
    public Binding directBindingB(Queue directQueueB, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueueB)
                .to(directExchange).with(RabbitMQConstant.DIRECT_ROUTING_NAME);
    }
}
